package com.cyandpos.pad.offline.model;

import android.database.Cursor;

import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.offline.offlinedb.CursorUtil;
import com.cyandpos.pad.offline.offlinedb.Sqldb;
import com.cyandpos.pad.posdata.ConfigInfo;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.LoadConfig;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.Permission;
import com.cyandpos.pad.posdata.Person;
import com.cyandpos.pad.posdata.SktInfo;
import com.cyandpos.pad.posdata.Sktconfig;
import com.cyandpos.pad.utils.Md5Util;
import com.cyandpos.pad.utils.log.MessageLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OfflineLogin {

    public static void login(Sqldb sqldb,Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        LoadConfig loadConfig=new LoadConfig();
        loadConfig.setSkt(getSktInfo(sqldb,params));
        loadConfig.setConfigs(getConfigs(sqldb,params));
        passWord(sqldb,params,callback,loadConfig);
    }

    /**
     * 获取款台信息
     */
    public static SktInfo getSktInfo(Sqldb sqldb, Map<String, Object> params){
        SktInfo skt=new SktInfo();
        String mcName ="";
        int permit_bar =0;
        int permit_gz=0;
        int permit_zg=0;
        int csmcCode =-1;
        List<Sktconfig> sktconfigList=new ArrayList<>();
        try {
            Cursor cur=sqldb.find("SELECT A.CSMCDM,B.ID,B.CRMID,B.NAME,B.CCL,B.SXH,B.ZFSX,B.FDBH,B.URL_ZT,B.URL_POS,C.FDMC,C.FIELD1,C.FIELD2,C.FIELD3 FROM SKT A,SKTGROUP B,FDDEF C WHERE A.SKTNO="+"'"+params.get("sktno")+"'"+" AND A.GROUPID=B.ID AND C.FDBH=A.FDBH AND C.FDBH="+"'"+params.get("storeCode")+"'");
            MessageLog.writeLog("SELECT A.CSMCDM,B.ID,B.CRMID,B.NAME,B.CCL,B.SXH,B.ZFSX,B.FDBH,B.URL_ZT,B.URL_POS,C.FDMC,C.FIELD1,C.FIELD2,C.FIELD3 FROM SKT A,SKTGROUP B,FDDEF C WHERE A.SKTNO="+params.get("sktno")+" AND A.GROUPID=B.ID AND C.FDBH=A.FDBH AND C.FDBH="+params.get("storeCode"));
            if (cur != null && cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    mcName = CursorUtil.getString(cur, "FDMC");
                    csmcCode = CursorUtil.getInt(cur, "CSMCDM");
                    if (csmcCode > 0) {
                        Cursor cur1 = sqldb.find("select DEPT_NAME from BM WHERE DEPTID=" + csmcCode);
                        MessageLog.writeLog("select DEPT_NAME from BM WHERE DEPTID=" + csmcCode);
                        if (cur1 != null && cur1.getCount() > 0) {
                            while (cur1.moveToNext()) {
                                mcName = CursorUtil.getString(cur1, "DEPT_NAME");
                            }
                            cur1.close();
                        }
                    }
                }
                cur.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }

        try {

            Cursor cur2 = sqldb.find("SELECT C.SXLX,C.SXVAL,C.CONTENT FROM SKT A,SKTGROUP B,SKTGROUPITEM C,POS_DATA D  WHERE A.SKTNO=" + "'"+params.get("sktno") +"'" +"  AND A.GROUPID=B.ID  AND C.SXVAL=D.ID AND C.SXLX=D.LX AND A.GROUPID= C.ID AND D.STATUS=1");
            MessageLog.writeLog("SELECT C.SXLX,C.SXVAL,C.CONTENT FROM SKT A,SKTGROUP B,SKTGROUPITEM C,POS_DATA D  WHERE A.SKTNO=" + params.get("sktno") + "  AND A.GROUPID=B.ID  AND C.SXVAL=D.ID AND C.SXLX=D.LX AND A.GROUPID= C.ID AND D.STATUS=1");
            if (cur2 != null && cur2.getCount() > 0) {
                while (cur2.moveToNext()) {
                    Sktconfig item = new Sktconfig();
                    item.setLx(CursorUtil.getString(cur2, "SXLX"));
                    item.setId(CursorUtil.getString(cur2, "SXVAL"));
                    item.setValue(CursorUtil.getString(cur2, "CONTENT"));
                    sktconfigList.add(item);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
        for(int i=0;i<sktconfigList.size();i++){
            Sktconfig item=sktconfigList.get(i);
            if ("2".equals(item.getLx()) && "2".equals(item.getId())) {
                if ("2".equals(item.getValue())) {
                    permit_bar = 1;
                } else if ("1".equals(item.getValue())) {
                    permit_bar = 1;
                }
            }
            if ("2".equals(item.getLx()) && "1".equals(item.getId())) {
                if(item.getValue()!=null&&!"".equals(item.getValue())&&!"null".equals(item.getValue())) {
                    permit_gz = Integer.parseInt(item.getValue());
                }
            }
            if ("3".equals(item.getLx()) && "2".equals(item.getId())) {
                if(item.getValue()!=null&&!"".equals(item.getValue())&&!"null".equals(item.getValue())) {
                    permit_zg = Integer.parseInt(item.getValue());
                }
            }
        }
        skt.setSktno(params.get("sktno")+"");
        skt.setCsmcCode(csmcCode);
        skt.setMcName(mcName);
        skt.setPermit_bar(permit_bar);
        skt.setPermit_gz(permit_gz);
        skt.setPermit_zg(permit_zg);
        skt.setSktconfigList(sktconfigList);
        return  skt;
    }

    /**
     * 密码验证
     */
    public static void passWord(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback, LoadConfig loadConfig){
        try {
            Cursor cur3 = sqldb.find("select A.LOGIN_PASSWORD,A.OPER_STATION,A.PERSON_ID from XTCZY A,RYXX B where A.PERSON_ID=B.PERSON_ID and B.RYDM=" + "'"+params.get("personCode")+"'");
            MessageLog.writeLog("select A.LOGIN_PASSWORD,A.OPER_STATION,A.PERSON_ID from XTCZY A,RYXX B where A.PERSON_ID=B.PERSON_ID and B.RYDM=" + params.get("personCode"));
            if (cur3 != null && cur3.getCount() > 0) {
                if (cur3.moveToNext()) {
                    String decryptPass_temp = CursorUtil.getString(cur3, "LOGIN_PASSWORD");
                    String personId = CursorUtil.getString(cur3, "PERSON_ID");
                    String password = params.get("password") + "";
                    if (!"".equals(decryptPass_temp) && decryptPass_temp != null) {
                        password = Md5Util.md5(password);
                        if (!password.equals(decryptPass_temp)) {
                            callback.onFailure("密码不正确，请重新输入密码");
                        } else {
                            getCashier(sqldb, params, callback, loadConfig);
                            loadConfig.setPermissionsList(getPermissionsList(sqldb, personId, loadConfig));
                            loadConfig.setPayList(getPayList(sqldb, params));
                            callback.onSuccess(CursorUtil.getOfflineBean(loadConfig));
                        }
                    } else {
                        callback.onFailure("没有找到人员(" + params.get("personCode") + ")");
                    }
                } else {
                    callback.onFailure("没有找到人员(" + params.get("personCode") + ")");
                }
                cur3.close();
            } else {
                callback.onFailure("没有找到人员(" + params.get("personCode") + ")");
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
    }

    /**
     * 获取部门信息
     */
    public static void getCashier(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback, LoadConfig loadConfig){
        String cashierDeptId="";//人员部门ID
        try {
            Cursor cur5 = sqldb.find("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.RYDM=" + "'"+params.get("personCode") +"'"+ "  and (B.FDBH=" + "'"+params.get("storeCode") + "'"+" or B.FDBH=-1) and R.STATUS=1");
            MessageLog.writeLog("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.RYDM=" + params.get("personCode") + "  and (B.FDBH=" + params.get("storeCode") + " or B.FDBH=-1) and R.STATUS=1");
            if (cur5 != null && cur5.getCount() > 0) {
                while (cur5.moveToNext()) {
                    loadConfig.setCashierCode(CursorUtil.getString(cur5, "RYDM"));
                    loadConfig.setCashierId(CursorUtil.getInt(cur5, "PERSON_ID") + "");
                    loadConfig.setCashierName(CursorUtil.getString(cur5, "PERSON_NAME"));
                    loadConfig.setCashierDeptCode(CursorUtil.getString(cur5, "BMDM"));
                    cashierDeptId = CursorUtil.getInt(cur5, "DEPTID") + "";
                    loadConfig.setCashierDeptId(cashierDeptId);
                    loadConfig.setCashierDeptName(CursorUtil.getString(cur5, "DEPT_NAME"));
                }
                loadConfig.setPersonList(getPersonList(sqldb, cashierDeptId, loadConfig));
                loadConfig.setGoodsList(getGoodLst(sqldb, cashierDeptId, params));
                cur5.close();
            } else {
                callback.onFailure("没有找到人员(" + params.get("personCode") + ")");
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
    }

    /**
     * 获取人员列表
     * @param sqldb
     * @param cashierDeptId
     * @param loadConfig
     */
    public static  List<Person>  getPersonList(Sqldb sqldb, String cashierDeptId, LoadConfig loadConfig){
        List<Person> personList=new ArrayList<>();
        try {

            Cursor cur6 = sqldb.find("select A.PERSON_ID,A.PERSON_NAME,A.RYDM,A.DEPTID ,B.DEPT_NAME,A.SHOPID,B.BMDM from RYXX A,BM B where A.DEPTID=B.DEPTID AND B.STATUS =0 AND WORK_TYPE =2  AND A.DEPTID =" + cashierDeptId);
            MessageLog.writeLog("select A.PERSON_ID,A.PERSON_NAME,A.RYDM,A.DEPTID ,B.DEPT_NAME,A.SHOPID,B.BMDM from RYXX A,BM B where A.DEPTID=B.DEPTID AND B.STATUS =0 AND WORK_TYPE =2  AND A.DEPTID =" + cashierDeptId);
            if (cur6 != null && cur6.getCount() > 0) {
                while (cur6.moveToNext()) {
                    Person person = new Person();
                    person.setPersonId(CursorUtil.getInt(cur6, "PERSON_ID"));
                    person.setPersonName(CursorUtil.getString(cur6, "PERSON_NAME"));
                    person.setPersonCode(CursorUtil.getString(cur6, "RYDM"));
                    person.setPersonDeptId(CursorUtil.getInt(cur6, "DEPTID"));
                    person.setPersonDeptCode(CursorUtil.getString(cur6, "BMDM"));
                    person.setPersonDeptName(CursorUtil.getString(cur6, "DEPT_NAME"));
                    person.setShopId(CursorUtil.getInt(cur6, "SHOPID"));
                    personList.add(person);
                }
                cur6.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
        return personList;
    }

    /**
     * 获取权限列表
     * @param sqldb
     * @param personId
     * @param loadConfig
     */
    public static List<Permission> getPermissionsList(Sqldb sqldb, String personId, LoadConfig loadConfig){
        List<Permission> permissionsList = new ArrayList<>();
        try {
            Cursor cur7 = sqldb.find("select MSG_ID,VALUE,CAPTION,LX from XTCZY_POSQX where PERSON_ID =" + personId);
            MessageLog.writeLog("select MSG_ID,VALUE,CAPTION,LX from XTCZY_POSQX where PERSON_ID =" + personId);
            if (cur7 != null && cur7.getCount() > 0) {
                while (cur7.moveToNext()) {
                    Permission permission = new Permission();
                    permission.setCaption(CursorUtil.getString(cur7, "CAPTION"));
                    permission.setMsgId(CursorUtil.getString(cur7, "MSG_ID"));
                    permission.setValue(CursorUtil.getString(cur7, "VALUE"));
                    permissionsList.add(permission);
                }
                cur7.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
        return permissionsList;
    }

    /**
     * 收款方式列表
     */
    public static List<Payment> getPayList(Sqldb sqldb, Map<String, Object> params){
        List<Payment> payList=new ArrayList<>();
        try {
            Cursor cur8 = sqldb.find("select * from (select A.CODE,A.NAME,A.TYPE,A.YHQID,A.ZLCLFS,A.XSSX,A.HL,A.YEDY from SKFS A where A.CODE in (select g.SKFS from ZFSXDY_SKFS g, SKT s where s.SKTNO =" +"'"+ params.get("sktno") +"'" +"  and s.ZFSX = g.JLBH) UNION  select A.CODE,A.NAME,A.TYPE,A.YHQID,A.ZLCLFS,A.XSSX,A.HL,A.YEDY from SKFS A   where A.CODE in (select SKFSID from SKT_SKFS where SKTNO=" + "'"+params.get("sktno") + "'"+")) a order by a.XSSX,a.CODE");
            MessageLog.writeLog("select * from (select A.CODE,A.NAME,A.TYPE,A.YHQID,A.ZLCLFS,A.XSSX,A.HL,A.YEDY from SKFS A where A.CODE in (select g.SKFS from ZFSXDY_SKFS g, SKT s where s.SKTNO =" + params.get("sktno") + "  and s.ZFSX = g.JLBH) UNION  select A.CODE,A.NAME,A.TYPE,A.YHQID,A.ZLCLFS,A.XSSX,A.HL,A.YEDY from SKFS A   where A.CODE in (select SKFSID from SKT_SKFS where SKTNO=" + params.get("sktno") + ")) a order by a.XSSX,a.CODE");
            if (cur8 != null && cur8.getCount() > 0) {
                while (cur8.moveToNext()) {
                    Payment payment = new Payment();
                    payment.setPayCode(CursorUtil.getInt(cur8, "CODE"));
                    payment.setPayName(CursorUtil.getString(cur8, "NAME"));
                    payment.setPayType(CursorUtil.getInt(cur8, "TYPE"));
                    payment.setYhqId(CursorUtil.getInt(cur8, "YHQID"));
                    payment.setChangeType(CursorUtil.getInt(cur8, "ZLCLFS"));
                    payment.setXssx(CursorUtil.getInt(cur8, "XSSX"));
                    payList.add(payment);
                }
                cur8.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
        return payList;
    }

    /**
     * 获取商品列表
     * @param sqldb
     * @param cashierDeptId
     * @param params
     * @return
     */
    public static  List<Goods> getGoodLst(Sqldb sqldb, String cashierDeptId, Map<String, Object> params){
        List<Goods> goodsList=new ArrayList<>();
        try {
            Cursor cur9 = sqldb.find("SELECT * from (select NAME,SPCODE,BARCODE from GTSP B,SPXX C where B.SP_ID = C.SP_ID  AND C.STATUS =0   AND B.DEPTID =" + cashierDeptId + " AND C.FDBH =" +"'"+ params.get("storeCode") +"'" +"  ORDER BY BARCODE,SPFL )");
            MessageLog.writeLog("SELECT * from (select NAME,SPCODE,BARCODE from GTSP B,SPXX C where B.SP_ID = C.SP_ID  AND C.STATUS =0   AND B.DEPTID =" + cashierDeptId + " AND C.FDBH =" + params.get("storeCode") + "  ORDER BY BARCODE,SPFL )");
            if (cur9 != null && cur9.getCount() > 0) {
                while (cur9.moveToNext()) {
                    Goods goods = new Goods();
                    goods.setName(CursorUtil.getString(cur9, "NAME"));
                    goods.setBarCode(CursorUtil.getString(cur9, "BARCODE"));
                    goods.setCode(CursorUtil.getString(cur9, "SPCODE"));
                    goodsList.add(goods);
                }
                cur9.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
      return goodsList;
    }

    public static List<ConfigInfo> getConfigs(Sqldb sqldb, Map<String, Object> params){
        List<ConfigInfo> configs=new ArrayList<>();
//        try {
//            Cursor cur = sqldb.find("SELECT C.SXLX,C.SXVAL,C.CONTENT FROM SKT A,SKTGROUP B,SKTGROUPITEM C,POS_DATA D  WHERE A.SKTNO=" + "'"+params.get("sktno") +"'" +"  AND A.GROUPID=B.ID  AND C.SXVAL=D.ID AND C.SXLX=D.LX AND A.GROUPID= C.ID AND D.STATUS=1");
//            LocalLog.writeLog("SELECT C.SXLX,C.SXVAL,C.CONTENT FROM SKT A,SKTGROUP B,SKTGROUPITEM C,POS_DATA D  WHERE A.SKTNO=" + params.get("sktno") + "  AND A.GROUPID=B.ID  AND C.SXVAL=D.ID AND C.SXLX=D.LX AND A.GROUPID= C.ID AND D.STATUS=1");
//            if (cur != null && cur.getCount() > 0) {
//                while (cur.moveToNext()) {
//                    ConfigInfo configInfo = new ConfigInfo();
//                    configInfo.setId(CursorUtil.getInt(cur, "SXLX"));
//                    configInfo.setValue(CursorUtil.getInt(cur, "SXVAL") + "");
//                    configInfo.setDesc(CursorUtil.getString(cur, "CONTENT"));
//                    configs.add(configInfo);
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//            LocalLog.writeLog(e+"");
//        }

        try {
            Cursor cur = sqldb.find("SELECT * FROM BFCONFIG   WHERE JLBH=" +1101002002 );
            if (cur != null && cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    ConfigInfo configInfo = new ConfigInfo();
                    configInfo.setId(CursorUtil.getInt(cur, "JLBH"));
                    configInfo.setValue(CursorUtil.getString(cur, "CUR_VAL") + "");
                    configInfo.setDesc(CursorUtil.getString(cur, "LX"));
                    configs.add(configInfo);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
        return configs;
    }
}
